package leetcode7;

public class test23 {
    /*
    * 141. 环形链表*/
    //快慢指针
    public class Solution {
        public boolean hasCycle(ListNode head) {
            ListNode slow = head, fast = head;
            while (fast != null && fast.next != null) {
                fast = fast.next.next;
                slow = slow.next;
                if (slow == fast)
                    return true;
            }
            return false;

        }
    }
    //哈希表
    public boolean hasCycle(ListNode head) {
        Set<ListNode> set = new HashSet<>();
        ListNode ptr = head;
        while(ptr != null){
            if(set.contains(ptr)){
                return true;
            }
            set.add(ptr);
            ptr = ptr.next;
        }
        return false;
    }
}
